<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_ProductBuy extends Zend_Db_Table{
	
	protected $_name    = "products";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['id']) && $this->checkExists($data['id'])){
                $productID = $id = $data['id'];
                
                $db->update("product_buys", $data, "id = $id");
            }else{
                $db->insert('product_buys', $data);
                $productID = $db->lastInsertId("product_buys");
            }
            
            $db->commit();
            
            return $productID;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getDatas($supplier_id = null){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('p' => 'product_buys'), array('id', 'name', 'net', 'type', 'supplier', 'price as price', 'quantity'))
                    ->joinLeft(array('b' => 'brands'), "p.brand = b.id", array('brand'))
                    ->joinLeft(array('s' => 'suppliers'), "s.id = p.supplier", array('name AS supplier'))
                    ->order('p.brand')
                    ->order('p.name')
                    ->order('p.id')
                    ->where("p.deleted = 0");
        
        if($supplier_id != null){
            $selectNotIn = $db->select()
                                ->from(array('pd' => 'product_delete'), array('product_id'))
                                ->where('pd.supplier_id = ?', $supplier_id);
            
            $select->where("(supplier = $supplier_id OR showallcustomer = 1) AND p.id NOT IN ($selectNotIn)");
        }
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($id, $join = false){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('p' => 'product_buys'), array('id', 'name', 'net', 'brand', 'type', 'supplier', 'price', 'showinsale', 'showallcustomer'))
                        ->where('p.id = ?', $id, 'INTEGER');
            
            if($join){
                $select->joinLeft(array('b' => 'brands'), "b.id = p.brand", array('brand as brand_name'));
            }
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $data = array();
            $data['id'] = $id;
            $data['deleted'] = 1;
            
            $db->update("product_buys", $data, "id = $id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function deleteDataForSupplier($supplier_id, $product_id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $data = array();
            $data['product_id'] = $product_id;
            $data['supplier_id'] = $supplier_id;
            
            $db->insert("product_delete", $data);
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkExists($id){
        $option = array('table' => 'product_buys', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}